#pragma once
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/ftp.h>
#include <arpa/inet.h>

#include <iostream>
#include <string>
#include <sstream>
#include <unordered_set>
class InetAddr{

    struct sockaddr_in _addr;
    uint16_t _port;
    std::string _ip;
    public:
    InetAddr(struct sockaddr_in & addr)
    :_addr(addr)
    {
        _port = ntohs(addr.sin_port);
        // _ip = inet_ntoa(addr.sin_addr);//这个函数可能是存在线程安全的，因为返回的是char*，那么就会可能多线程访问出现覆盖的情况
        char buffer[64];
        inet_ntop(AF_INET,&addr.sin_addr,buffer,sizeof(buffer));  
        _ip = buffer; 
    }

    std::string InetAddr_to_string()const 
    {
        std::stringstream sstr;
        sstr<<"Client ip:"<<_ip<<" | Client_port: "<<_port;
        return sstr.str(); 
    }
    bool operator==(const InetAddr& addr)const  
    {
        return this->_ip == addr._ip && this->_port==addr._port;
    }
    const struct sockaddr_in& GetAddr()const 
    {
        return _addr;
    }
    uint16_t Getip()const 
    {
        return _addr.sin_addr.s_addr;
    }
    uint16_t GetPort()const
    {
        return _port;
    }
};

struct InetAddrHash{
    size_t operator()(const InetAddr& addr)const
    {
        return std::hash<int>()(addr.Getip()) ;
    }
};

